package cn.com.bsoft.cloud.utils.test;


import cn.com.bsoft.cloud.utils.AesUtils;
import org.apache.tomcat.util.codec.binary.Base64;

import java.util.Map;

public class RSATester {

    static String publicKey;
    static String privateKey;

    static {
        try {
            Map<String, Object> keyMap = RSAUtils.genKeyPair();
            publicKey = RSAUtils.getPublicKey(keyMap);
            privateKey = RSAUtils.getPrivateKey(keyMap);
            System.err.println("公钥: \n\r" + publicKey);
            System.err.println("私钥： \n\r" + privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        test();
        testSign();
    }

    static void test() throws Exception {

        String pass = "pass";
        System.err.println("公钥加密——私钥解密");
        String source = "{     \"startDate\": \"2018-12-01 00:00:00\",  \t \"endDate\": \"2020-12-01 10:02:04\",  \t \"hospitalCodes\":[\"12061\",\"12071\"],  \t \"pharmaceuticalCompanyCode\":\"321312\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\" ,  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\"}";
        System.out.println("\r加密前文字：\r\n" + source);
        byte[] aesData = AesUtils.encrypt(source,pass);
//        byte[] data = source.getBytes();
        byte[] encodedData = RSAUtils.encryptByPublicKey(aesData, publicKey);

        String  bbbb = Base64.encodeBase64String(encodedData);
        System.out.println("加密后文字：\r\n" + bbbb);
        byte[] bb = Base64.decodeBase64(bbbb);
        byte[] decodedData = RSAUtils.decryptByPrivateKey(bb, privateKey);
        byte[] aseDecData =AesUtils.decrypt(decodedData,pass);

        String target = new String(aseDecData);
        System.out.println("解密后文字: \r\n" + target);
    }

    static void testSign() throws Exception {
        String pass ="pass";
//        String prk="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKaaxCPCpNELAdqBrSRvx6N7Xig/\n" +
//                "8icsMPT+vn7dJ/bJRgM+UlxomNSdw7zBPzAaYpxMCKeQZQ7ueViD72nUa7tH+Y7jH2PNosGTYkVv\n" +
//                "9sm8xbodVQTJbpOSWHeMLQYkHByUrEzLJG/taBWYm0vznTNcVKWGNa/SXTWD+oub3gP9AgMBAAEC\n" +
//                "gYAXkh3d0VPwH1aLy3zrgsUD5BhWKXXhBOeIQeJBoJhe2esF2nNcj4/PxOJEejKuY40//6HZ0xv5\n" +
//                "GNQiKchdeKglA7QMLXrJ2N5GhXQb05XCNZoEvP7cL19Kb9bnSQRuWyXFoo89n6ZjpJPv/55Uzk8K\n" +
//                "wp3FNvWWxtLQOmUlsmOLIQJBAPO/vu5bCT1WRuDP+bSr3/K6SRtCUy7V4Bpk2gaoOr4c3HPnj0dH\n" +
//                "zFMZYhoDbfevhCyUptdVtwxdFOM1IvKkJt8CQQCu+mxMCw3RO+4dZ6FxCH0+jXmmTvYgIgnbT4aN\n" +
//                "Xihmi7Vn+mi6TvbK6aHyYWiRlDLJBq5fgGSpdoucbQ3kYzyjAkEAjR+fM/GRT+n77316XZiFeMtS\n" +
//                "OG4WKy+5KI7rjJ8qI5giKxkOY362VgaEPftuV9FO/WCzmEkQSiA6T7IcIc/YkQJAOC+JXtGd2jXY\n" +
//                "O/Du0hDbxp4/Fbv9JExJxfY/Sl2Vs3fhBEZQIqMAdB+ZNO86WKSxugM/MsNTj2bVZaFO+Ks06QJB\n" +
//                "AOoWA30cLjUGL7SphMfjiCMtJ4gWA/9hk+gkZEYQ2Pk3e78/E+MWfquK/hnIkhTFniU5tdXCil2r\n" +
//                "9RCbANb3i38=";
//
//        String puk="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmmsQjwqTRCwHaga0kb8eje14oP/InLDD0/r5+\n" +
//                "3Sf2yUYDPlJcaJjUncO8wT8wGmKcTAinkGUO7nlYg+9p1Gu7R/mO4x9jzaLBk2JFb/bJvMW6HVUE\n" +
//                "yW6Tklh3jC0GJBwclKxMyyRv7WgVmJtL850zXFSlhjWv0l01g/qLm94D/QIDAQAB";
        String prk="MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALBj+0bUczodxxEdOOrHDHZFOkU3DUsoSzq1uBn3yvPTP9AftbbDCsFL9n5lPAsvnoHyuL5YwcroLij+BOb94lQwgNMtahuXfzfHXNkgdkpdtabJvFIpF3k2s5+ldiclr3f+cth3xBuHCbBOFzfDFKupbOehaBPHZIMNneFxPcxVAgMBAAECgYEAi9k5SBb4pIMnaTI6Bip6fuq0IFxdddEklA+vPydvCZHbbi816wkIZejMLboluNiwW2cNghzsyOr8bhO330w7PB8aQ3cRL1aC2YP/81u6qZcdVrCMN4rBEuc9rNIjqGwX4D1l3oI0zboc/qliEKKZ890fWhnc4UJxCHCq07D8J6ECQQD6VkvfoMJfpSO/rLaZWoHsTeo+FBkb1xfUo/W4UX5Vc/OuOfe7Hr9Bj5LA3ax9YnPGeVbELEiYmM3K3jsP6vaDAkEAtGF11iOn7mk7r+dbvXiyeujUy89wtfHCV50GbyGcoqrXupKvba53Mncx5dNzDGncLCslDvDZuvGmgtxSkLN6RwJBAJhNaFvlslv+XIGA0pb6OZFK1fhuwdfuRNFZ72T0L6hNozOckNuHGVUJxH8SXp7qBwX5TJCNYJ12Wb3S9pv4l4ECQEGvT9K3zzoxFewbSrv3TL8XNA43ruqCa+utoZ8sVTajEcW+DqXZJ0y4PPJdCoMVtXhcCbWkHRVHVARJKMPKp5cCQQCV0wZ48jcHAI85pQkOxeUgnwh7PMuk2KkQ/RnhWIMZWqHeKBDJVO/Z6Mh7cpzjGrwlvFk1Bndbn4FbXxaquuEp";


        String puk="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwY/tG1HM6HccRHTjqxwx2RTpFNw1LKEs6tbgZ98rz0z/QH7W2wwrBS/Z+ZTwLL56B8ri+WMHK6C4o/gTm/eJUMIDTLWobl383x1zZIHZKXbWmybxSKRd5NrOfpXYnJa93/nLYd8QbhwmwThc3wxSrqWznoWgTx2SDDZ3hcT3MVQIDAQAB";

        System.err.println("私钥加密——公钥解密");
//        String source = "{     \"startDate\": \"2018-12-01 00:00:00\",  \t \"endDate\": \"2020-12-01 10:02:04\",  \t \"hospitalCodes\":[\"12061\",\"12071\"],  \t \"pharmaceuticalCompanyCode\":\"321312\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\" ,  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\"}";
//        String source = "{\"startDate\": \"2000-12-01 00:00:00\",\"endDate\": \"2020-12-01 10:02:04\",\"hospitalCodes\":[\"424860135\"],\"pharmaceuticalCompanyCode\":\"321312\",\"pharmaceuticalCompanyName\":\"xxxx医药公司\"}";
        String sources ="{     \"startDate\": \"2015-12-01 00:00:00\",  \t \"endDate\": \"2020-12-01 10:02:04\",  \t \"hospitalCodes\":[\"424860135\"],  \t \"pharmaceuticalCompanyCode\":\"321312\",  \t \"pharmaceuticalCompanyName\":\"xxxx医药公司\" }";
        String source =sources.replace(" ","").replace("\t","");
        System.out.println("原文字：\r\n" + source);
        byte[] data = source.getBytes("utf-8");
//        byte[] aesData = AesUtils.encrypt(source,pass);
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, prk);
        String  bbbb = Base64.encodeBase64String(encodedData);
        System.out.println("加密后：\r\n" + bbbb);
//        byte[] tttttt = Base64.encodeBase64String(encodedData).getBytes();
//        String ttt
        byte[] bb = Base64.decodeBase64(bbbb);
        byte[] decodedData = RSAUtils.decryptByPublicKey(bb, puk);
//        byte[] aseDecData =AesUtils.decrypt(decodedData,pass);
        String target = new String(decodedData);
        System.out.println("解密后: \r\n" + target);
        System.err.println("私钥签名——公钥验证签名");


        System.err.println("签名:\r" );
        String sign = RSAUtils.sign(source.getBytes(), prk);
        System.err.println("签名:\n" + sign);
        boolean status = RSAUtils.verify(source.getBytes(), puk, sign);
        System.err.println("验证结果:\n" + status);
    }

}
